package cn.aishua.util;

import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class FormatConverter
{

    public static String toPercentageFormat( String strFraction )
    {

        try
        {
            float flFraction = Float.valueOf( strFraction );
            NumberFormat nt = NumberFormat.getPercentInstance();
            nt.setMaximumFractionDigits( 1 );
            String rate = nt.format( flFraction );
            return rate;
        }
        catch( Exception e )
        {
            return "0%";
        }

    }

    public static int toInt( String str )
    {

        try
        {
            int value = Integer.valueOf( str );
            return value;
        }
        catch( Exception e )
        {
            return 0;
        }

    }

    public static Double toDouble( String strDouble )
    {
        try
        {
            return Double.valueOf( strDouble );
        }
        catch( Exception e )
        {
            return 0.0;
        }
    }

    public static String toIntFormat( String strFraction )
    {
        try
        {
            float flFraction = Float.valueOf( strFraction );
            NumberFormat nt = NumberFormat.getIntegerInstance();
            String rate = nt.format( flFraction );
            rate = rate.replace( ",", "" );
            return rate;
        }
        catch( Exception e )
        {
            return "0";
        }
    }

    /**
     * card number format: two spaces after every for digital
     * 
     * @param cardNum
     * @return
     */
    public static String toCardNumFormat( String cardNum )
    {
        StringBuffer sbCardNum = new StringBuffer( cardNum );
        {
            for( int i = 4; i < sbCardNum.length(); i += 6 )
            {
                sbCardNum.insert( i, "  " );
            }
        }

        return sbCardNum.toString();
    }

    public static boolean isNotEmptyString( String str )
    {
        return str != null && !"".equals( str.trim() );
    }

    public static String getJsonString( JSONObject jsonObj, String name, String defaultValue ) throws JSONException
    {
        // No string with the given name exists in this json object
        if( jsonObj.optString( name ).equals( "" ) )
        {
            Log.w( "json", "no item with " + name + " in this json object" );
            return defaultValue;
        }
        else if( jsonObj.get( name ) == JSONObject.NULL )
        {
            Log.d( "json", "item " + name + " is null" );
            return defaultValue;
        }

        return jsonObj.getString( name );
    }

    public static boolean isChinese( char c )
    {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of( c );
        if( ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
            || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
            || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
            || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
            || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS )
        {
            return true;
        }
        return false;
    }

    public static String ToDBC( String input )
    {
        char[] c = input.toCharArray();
        for( int i = 0; i < c.length; i++ )
        {
            if( c[i] == 12288 )
            {
                c[i] = (char) 32;
                continue;
            }
            if( c[i] > 65280 && c[i] < 65375 )
                c[i] = (char) ( c[i] - 65248 );
        }
        return new String( c );
    }

    public static String stringFilter( String str )
    {
        str = str.replaceAll( "：", ": " ).replaceAll( "、", ", " ).replaceAll( "；", "; " );
        return str.trim();
    }
}
